package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UserKit;
import com.sinbader.xiaole.common.entity.Shelf;
import com.sinbader.xiaole.common.enums.NumberUtilEnum;
import com.sinbader.xiaole.common.mapper.ShelfMapper;
import com.sinbader.xiaole.common.model.params.ShelfParam;
import com.sinbader.xiaole.common.model.result.ShelfResult;
import  com.sinbader.xiaole.common.service.ShelfService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinbader.xiaole.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 货架 服务实现类
 * </p>
 *
 * @author LuoYong
 * @since 2020-04-17
 */
@Service
public class ShelfServiceImpl extends ServiceImpl<ShelfMapper, Shelf> implements ShelfService {

    @Override
    public void add(ShelfParam param){
        Shelf entity = getEntity(param);
        entity.setShelvesNum(StringUtil.getIDNumber(NumberUtilEnum.货架.getName()));
        StringBuilder builder = new StringBuilder();
        builder.append(entity.getRegion());
        builder.append("-");
        builder.append(entity.getRow());
        builder.append("-");
        builder.append(entity.getFloor());
        builder.append("-");
        builder.append(entity.getMark());
//        builder.append("");
//        entity.setn
        entity.setShelvesName(builder.toString());
        entity.setCreateTime(new Date());
        entity.setCreateUser(UserKit.getUserId());
        this.save(entity);
    }

    @Override
    public void delete(ShelfParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShelfParam param){
        StringBuilder builder = new StringBuilder();
        builder.append(param.getRegion());
        builder.append("-");
        builder.append(param.getRow());
        builder.append("-");
        builder.append(param.getFloor());
        builder.append("-");
        builder.append(param.getMark());
        param.setShelvesName(builder.toString());
        Shelf oldEntity = getOldEntity(param);
        Shelf newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        newEntity.setUpdateUser(UserKit.getUserId());
        this.updateById(newEntity);
    }

    @Override
    public ShelfResult findBySpec(ShelfParam param){
        return null;
    }

    @Override
    public List<ShelfResult> findListBySpec(ShelfParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShelfParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShelfParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Shelf getOldEntity(ShelfParam param) {
        return this.getById(getKey(param));
    }

    private Shelf getEntity(ShelfParam param) {
        Shelf entity = new Shelf();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
